package com.xmy.pool;

import java.util.concurrent.*;

/**
 * 四大拒绝策略
 *  ThreadPoolExecutor.AbortPolicy();
 *  ThreadPoolExecutor.CallerRunsPolicy();
 *  ThreadPoolExecutor.DiscardPolicy();
 *  ThreadPoolExecutor.DiscardOldestPolicy();
 */
public class Demo01 {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        //自定义线程池，工作中只会用ThreadPoolExecutor
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());
        try {
            //最大承载线程数量：阻塞队列BlockingDeque的容量+线程池的最大容量maxPoolSize
            for (int i = 1; i <=9 ; i++) {
                //使用线程池来创建线程
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok!");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        threadPool.shutdown();
    }
}
